package com.mcr.graduateprogram.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.graduateprogram.bean.GameSpecification;
import com.mcr.graduateprogram.common.R;
import com.mcr.graduateprogram.service.IGameSpecificationService;
import com.mcr.graduateprogram.service.IPhotoUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author graduateprogram
 * @since 2023-02-25
 */
@Api(tags = "游戏规格相关接口")
@CrossOrigin
@RestController
@RequestMapping("/graduateprogram/game-specification")
public class GameSpecificationController {
    @Autowired
    private IGameSpecificationService iGameSpecificationService;
    @Autowired
    private IPhotoUpload iPhotoUpload;
    @Value("${qiniu.game}")
    private String game;
    @GetMapping("getPage")
    @ApiOperation(value = "分页查询具体规格游戏")
    public R getPage(@RequestParam(value = "page")Integer page,
                     @RequestParam(value = "pageSize")Integer pageSize,
                     @RequestParam(value = "gameId",required = false)Long gameId){
        Page<GameSpecification> gameSpecificationPage=iGameSpecificationService.getPage(page,pageSize,gameId);
        return R.success(gameSpecificationPage);
    }
    @ApiOperation(value = "根据Id修改游戏版本")
    @PostMapping("updateById")
    @CacheEvict(value = "graduationProgram",key = "'allGameSpaceification'")//删缓存allGameSpaceification
    public R updateById(@RequestBody GameSpecification gameSpecification){
        iGameSpecificationService.updateById(gameSpecification);
        return R.success(null);
    }
    @ApiOperation(value = "新添游戏版本")
    @PostMapping("addNew")
    @CacheEvict(value = "graduationProgram",key = "'allGameSpaceification'")//删缓存allGameSpaceification
    public R addNew(@RequestBody GameSpecification gameSpecification){
        iGameSpecificationService.save(gameSpecification);
        return R.success(null);
    }
    @ApiOperation(value = "上传游戏图片")
    @PostMapping("uploadPhoto")
    public R uploadPhoto(@RequestPart("photo") MultipartFile photo){
        return R.success(iPhotoUpload.photoUpload(photo,game));
    }
    @ApiOperation(value = "下载激活码进货模板")
    @GetMapping("downloadExcelTemplate")
    public void downloadExcelTemplate(@RequestParam(value = "id")Long id,
                                   @RequestParam(value = "number")Integer number,
                                   HttpServletResponse response){
        iPhotoUpload.downloadExcel(id,number,response);
    }
    @ApiOperation(value = "查询所有规格游戏")
    @GetMapping("getAll")
    @Cacheable(value = "graduationProgram",key = "'allGameSpaceification'")//从缓存取数据或存入缓存allGameSpaceification
    public R getAll(){
        return R.success(iGameSpecificationService.list());
    }
}
